package org.example.web.admin.service.impl;

import io.minio.*;
import org.example.common.config.MinioProperties;
import org.example.web.admin.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String upload(MultipartFile file) {
        try {
            //判断bucket是否存在
            boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(minioProperties.getBucketname())
                    .build());
            //bucket不存在，创建
            if (!bucketExists) {
                //创建bucket
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(minioProperties.getBucketname()).build());
                //设置bucket策略：私有、公共、自定义
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                        .bucket(minioProperties.getBucketname())
                        .config(createBucketPolicyConfig(minioProperties.getBucketname())).build());
            }

            //String filename = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + UUID.randomUUID() + "-" + file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString();
            String filename = uuid + "-" + file.getOriginalFilename();
            // UUID-01.jpg
            //上传文件到minio
            minioClient.putObject(PutObjectArgs.builder().
                    bucket(minioProperties.getBucketname()). //bucket名称
                            object(filename). //在bucket文件名称
                            stream(file.getInputStream(), file.getSize(), -1).
                    contentType(file.getContentType()).build());

            //返回文件在minio里面地址
            // http://127.0.0.1:9000/atguigu/01.jpg
//            String url = properties.getEndpoint()
//                    +"/"+properties.getBucketname()
//                    +"/"+filename;

            return String.join("/", minioProperties.getEndpoint(),
                    minioProperties.getBucketname(), filename);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String createBucketPolicyConfig(String bucketName) {
        return """
                {
                  "Statement" : [ {
                    "Action" : "s3:GetObject",
                    "Effect" : "Allow",
                    "Principal" : "*",
                    "Resource" : "arn:aws:s3:::%s/*"
                  } ],
                  "Version" : "2012-10-17"
                }
                """.formatted(bucketName);
    }
}
